package com.asset.salesforce.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.converter.AppointmentConverter;
import com.asset.salesforce.domain.Appointment;
import com.asset.salesforce.domain.Call;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Post;
import com.asset.salesforce.domain.UpdateChatter;
import com.asset.salesforce.enums.FOLLOWTYPE;
import com.asset.salesforce.enums.UPDATETYPE;
import com.asset.salesforce.form.AppointmentForm;
import com.asset.salesforce.form.CallForm;
import com.asset.salesforce.service.AppointmentService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CheckingUtilsService;
import com.asset.salesforce.service.CompanyService;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.PostService;
import com.asset.salesforce.service.UpdateChatterService;

@Controller
public class AppointmentController {

	@Autowired
	private AppointmentService appointmentService;
	@Autowired
	private JoinRequestService joinRequestService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private ContactService contactService;
	@Autowired
	private CalendarService calendarService;
	@Autowired
	private CheckingUtilsService checkingUtilsService;
	@Autowired
	private PostService postService;
	@Autowired
	private UpdateChatterService updateChatterService;
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/new-appointment",method=RequestMethod.GET)
	public String newAppointment(Model model,HttpSession session,HttpServletRequest request, ModelMap modelMap) {
		if (SecurityUtils.getSubject().hasRole("appointmentAdd")){
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Integer parentId = Integer.parseInt(request.getParameter("parentId").toString());
		String parentType =request.getParameter("parentType").toString();
		List<Contact> contacts = contactService.getContactByParent(parentType, parentId);
		String companyName = userDetails.getCompany().getCompanyName();
		List<Employee> salesMen = companyService.getActivatedEmployeesByCompanyName(companyName);
		
		if(salesMen != null){
			for (Employee employee : salesMen) {
				employee.setIsFollowed(checkingUtilsService.checkIfCurrentFollowThatPeople(userDetails, employee));
//				if(!employee.getIsOwner()){
//					if(!employee.getIsActive()){
//						companyEmployees.remove(employee);
//					}
//				}
			}
		}
		AppointmentForm appointmentForm = new AppointmentForm();
		model.addAttribute("appointmentForm", appointmentForm);
		model.addAttribute("id",request.getParameter("id"));
		model.addAttribute("salesMen", salesMen);
		model.addAttribute("contacts", contacts);
		model.addAttribute("parentId", parentId);
		model.addAttribute("parentType", parentType);
		model.addAttribute("userDetails", userDetails);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "new-appointment";
		}
		else{
			return "accessDenied";
		}
	}
	
	@RequestMapping(value="/new-appointment",method=RequestMethod.POST)
	public String newAppointment(Model model,HttpSession session,HttpServletRequest request,
			
			@Valid  AppointmentForm appointmentForm,BindingResult result
			) {
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		String startTime = appointmentForm.getTime();
		String endTime = appointmentForm.getMeetingDuration();
		if(startTime!=null&&!(startTime.equalsIgnoreCase(""))){
			if(endTime!=null&&!(endTime.equalsIgnoreCase(""))){
				String[] TargetStartTimeForTime = startTime.split(":");
                Integer targetStartTime1 = (Integer.parseInt(TargetStartTimeForTime[0])*60)+Integer.parseInt(TargetStartTimeForTime[1]);
                
                String[] targetEndTimeForTime = endTime.split(":");
                Integer targetEndTime1 = (Integer.parseInt(targetEndTimeForTime[0])*60)+Integer.parseInt(targetEndTimeForTime[1]);
                Integer timeBetwwenThem = targetEndTime1-targetStartTime1;
				if(timeBetwwenThem<15){
		            result.rejectValue("time", "time","Please,Enter at leat 15 Minutes");
				}
			}
		}
		if(result.hasErrors()){
			
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>....."+result.getAllErrors().size());
			Integer parentId = Integer.parseInt(request.getParameter("parentId").toString());
			String parentType = request.getParameter("parentType").toString();
			List<Contact> contacts = contactService.getContactByParent(parentType, parentId);
			String companyName = userDetails.getCompany().getCompanyName();
			List<Employee> salesMen = companyService.getActivatedEmployeesByCompanyName(companyName);

			if(salesMen != null){
				for (Employee employee : salesMen) {
					employee.setIsFollowed(checkingUtilsService.checkIfCurrentFollowThatPeople(userDetails, employee));
//					if(!employee.getIsOwner()){
//						if(!employee.getIsActive()){
//							companyEmployees.remove(employee);
//						}
//					}
				}
			}
//			if(contacts == null){
//				contacts = (List<Contact>) new Contact();
//			}
			model.addAttribute("appointmentForm", appointmentForm);
			model.addAttribute("id",request.getParameter("id"));
			model.addAttribute("salesMen", salesMen);
			model.addAttribute("contacts", contacts);
			model.addAttribute("parentId", parentId);
			model.addAttribute("userDetails", userDetails);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "new-appointment";
		}
		Appointment appointment = new Appointment();
		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		format.setLenient(false);
		Date newDate;
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ "+appointmentForm.getTime()+"@@@@@@@@@@@@@@@@@@@ "+appointmentForm.getMeetingDuration());
		
		if(appointmentForm.getSalesMan() != null){
			appointment.setSalesMan(employeeService.getById(appointmentForm.getSalesMan()));
		}
		if(appointmentForm.getContactedPerson() != null){
			appointment.setContactedPerson(contactService.getContactById(appointmentForm.getContactedPerson()));
		}
		if(appointmentForm.getDay() != null){
			appointment.setDay(appointmentForm.getDay());
		}
		if(appointmentForm.getDate() != null){
			try {
				newDate = format.parse(appointmentForm.getDate());
				appointment.setDate(newDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(appointmentForm.getTime() != null){
			appointment.setTime(appointmentForm.getTime());
		}
		if(appointmentForm.getComment() != null){
			appointment.setComment(appointmentForm.getComment());
		}
		if(appointmentForm.getNextFollowUp() != null){
			appointment.setNextFollowUp(Boolean.valueOf(appointmentForm.getNextFollowUp()));
		}
		if(appointmentForm.getNextFollowUpType() != null){
			appointment.setFollowUpType(appointmentForm.getNextFollowUpType());
		}
		if(appointmentForm.getNextFollowUpDate() != null&&!(appointmentForm.getNextFollowUpDate().equals(""))){
			try {
				newDate = format.parse(appointmentForm.getNextFollowUpDate());
				appointment.setFollowUpDate(newDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(appointmentForm.getNextFollowUpDetails() != null){
			appointment.setFollowUpDetails(appointmentForm.getNextFollowUpDetails());
		}
		if(appointmentForm.getDiscountDone() != null){
			appointment.setDiscountDone(appointmentForm.getDiscountDone());
		}
		if(appointmentForm.getCompetitorOffer() != null){
			appointment.setCompatitorOffer(appointmentForm.getCompetitorOffer());
		}
		if(appointmentForm.getDealClosingDone() != null){
			appointment.setDealClosingDone(appointmentForm.getDealClosingDone());
		}
		if(appointmentForm.getReplyPeriod() != null){
			appointment.setReplyPeriod(appointmentForm.getReplyPeriod());
		}
		if(appointmentForm.getDealClosingReason() != null){
			appointment.setDealClosingReason(appointmentForm.getDealClosingReason());
		}
		if(appointmentForm.getMeetingDone() != null){
			appointment.setMeetingDone(appointmentForm.getMeetingDone());
		}
		if(appointmentForm.getMeetingDuration() != null){
			appointment.setMeetingDuration(appointmentForm.getMeetingDuration());
		}
		if(appointmentForm.getCustomerNeed() != null){
			appointment.setCustomerNeed(appointmentForm.getCustomerNeed());
		}
		if(appointmentForm.getDemoDone() != null){
			appointment.setDemoDone(appointmentForm.getDemoDone());
		}
		if(appointmentForm.getOfferSubmitted() != null){
			appointment.setOfferSubmitted(appointmentForm.getOfferSubmitted());
		}
		if(appointmentForm.getConfirmationDateExpected() != null){
			appointment.setConfirmationDateExpected(appointmentForm.getConfirmationDateExpected());
		}
		if(appointmentForm.getDiscountDone() != null){
			appointment.setDiscountDone(appointmentForm.getDiscountDone());
		}
		if(appointmentForm.getDealClosingReason() != null){
			appointment.setDealClosingReason(appointmentForm.getDealClosingReason());
		}
		appointment.setParentId(appointmentForm.getParentId());
		appointment.setParentType("lead");
		Date  date = new Date();
		appointment.setDateCreated(date);
		appointment.setDateModified(date);
		appointmentService.addAppointment(appointment);
		calendarService.appointmentEvent(appointment);
		//@ Hisham add next follow in calender table 
				if(appointmentForm.getNextFollowUpType() != null && appointmentForm.getNextFollowUpDate() != null &&!(appointmentForm.getNextFollowUpDate().equalsIgnoreCase("")) && appointmentForm.getNextFollowUpDetails() != null){
					try {
						Call call = new Call();
						call.setComment(appointmentForm.getNextFollowUpDetails());
						call.setDate(format.parse(appointmentForm.getNextFollowUpDate()));
//						call.setTime(appointmentForm.getTime());
//						call.setTime(appointmentForm.getMeetingDuration());
						
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				//update chatter
				UpdateChatter updateChatter = new UpdateChatter();
				updateChatter.setAction_emp(userDetails);
				updateChatter.setDate(new Date());
				updateChatter.setFollowedId(appointment.getId());
				updateChatter.setFollowType(FOLLOWTYPE.Appointment.getType());
				updateChatter.setUpdateType(UPDATETYPE.Added.getName());
				Post post = new Post();
				post.setPostDate(new Date());
				post.setPoster(userDetails);

				String postText = "<a href='/sales/s/view-appointment.html?id="+appointment.getId()+"'>i created Appointment at Time:"+appointment.getTime()+ "And Date:"+appointmentForm.getDate()+"</a>";
				post.setPostText(postText);
				postService.addPost(post);	
				updateChatter.setPost(post);
				updateChatterService.add(updateChatter);
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
		model.addAttribute("userDetails", userDetails);
		return "redirect:/s/calendar?empId="+userDetails.getId();
	}

	@RequestMapping(value="/edit-appointment",method=RequestMethod.GET)
	public String getEditAppointment(Model model,HttpSession session,HttpServletRequest request, ModelMap modelMap) {
		if (SecurityUtils.getSubject().hasRole("appointmentEdit")){
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Appointment appointment =appointmentService.getAppointmentById(new Appointment(),Integer.parseInt(request.getParameter("id"))); 
		AppointmentConverter appointmentConverter = new AppointmentConverter();
		AppointmentForm appointmentForm = appointmentConverter.appointmentConvertor(appointment);
		
		//Integer parentId = 1;

		Integer parentId =Integer.parseInt(request.getParameter("parentId").toString());
		String parentType =request.getParameter("parentType").toString();
		List<Contact> contacts = contactService.getContactByParent(parentType, parentId);
		String companyName = userDetails.getCompany().getCompanyName();
		List<Employee> salesMen = companyService.getActivatedEmployeesByCompanyName(companyName);

		if(salesMen != null){
			for (Employee employee : salesMen) {
				employee.setIsFollowed(checkingUtilsService.checkIfCurrentFollowThatPeople(userDetails, employee));
			}
		}
		if(contacts == null){
			contacts =  new ArrayList<Contact>();
		}
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+contacts.size());
		model.addAttribute("id",request.getParameter("id"));
		model.addAttribute("parentId",request.getParameter("parentId"));
		model.addAttribute("parentType",request.getParameter("parentType"));
		model.addAttribute("salesMen", salesMen);
		model.addAttribute("contacts", contacts);
		model.addAttribute("appointmentForm", appointmentForm);
		//model.addAttribute("parentId", parentId);
		model.addAttribute("userDetails", userDetails);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "edit-appointment";
		}
		else{
			return "accessDenied";
		}
	}

	@RequestMapping(value="/edit-appointment",method=RequestMethod.POST)
	public String postEditAppointment(Model model,HttpSession session,HttpServletRequest request,
			/*@RequestParam(value="salesMan",required =false) Integer salesMan,
			@RequestParam(value="contactedPerson",required =false) Integer contactedPerson,
			@RequestParam(value="day", required=false) String day,
			@RequestParam(value="date", required=false) String date,
			@RequestParam(value="time", required=false) String time,
			@RequestParam(value="comment", required=false) String comment,
			@RequestParam(value="nextFollowUp", required=false) Boolean nextFollowUp,
			@RequestParam(value="nextFollowUpType", required=false) String nextFollowUpType,
			@RequestParam(value="nextFollowUpDate", required=false) String nextFollowUpDate,
			@RequestParam(value="nextFollowUpDetails", required=false) String nextFollowUpDetails,
			@RequestParam(value="meetingDone", required=false) Boolean meetingDone,
			@RequestParam(value="meetingDuration", required=false) String meetingDuration,
			@RequestParam(value="customerNeed", required=false) String customerNeed,
			@RequestParam(value="demoDone", required=false) Boolean demoDone,
			@RequestParam(value="offerSubmitted", required=false) Boolean offerSubmitted,
			@RequestParam(value="confirmationDateExpected", required=false) String confirmationDateExpected,
			@RequestParam(value="discountDone", required=false) Boolean discountDone,
			@RequestParam(value="competitorOffer", required=false) Boolean competitorOffer,
			@RequestParam(value="dealClosingDone", required=false) Boolean dealClosingDone,
			@RequestParam(value="replyPeriod", required=false) String replyPeriod,
			@RequestParam(value="dealClosingReason", required=false) String dealClosingReason,
			@RequestParam(value="id", required=false) Integer id,
			@RequestParam(value="parentId", required=false) Integer parentId,
			@RequestParam(value="parentType", required=false) String parentType*/
			@Valid @ModelAttribute("appointmentForm") AppointmentForm appointmentForm,BindingResult result
			) {
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Integer parentId =Integer.parseInt(request.getParameter("parentId").toString());
		String parentType =request.getParameter("parentType").toString();
		List<Contact> contacts = contactService.getContactByParent(parentType, parentId);
		String companyName = userDetails.getCompany().getCompanyName();
		List<Employee> salesMen = companyService.getActivatedEmployeesByCompanyName(companyName);

		if(salesMen != null){
			for (Employee employee : salesMen) {
				employee.setIsFollowed(checkingUtilsService.checkIfCurrentFollowThatPeople(userDetails, employee));
			}
		}
		if(contacts == null){
			contacts =  new ArrayList<Contact>();
		}
		String startTime = appointmentForm.getTime();
		String endTime = appointmentForm.getMeetingDuration();
		if(startTime!=null&&!(startTime.equalsIgnoreCase(""))){
			if(endTime!=null&&!(endTime.equalsIgnoreCase(""))){
				String[] TargetStartTimeForTime = startTime.split(":");
                Integer targetStartTime1 = (Integer.parseInt(TargetStartTimeForTime[0])*60)+Integer.parseInt(TargetStartTimeForTime[1]);
                
                String[] targetEndTimeForTime = endTime.split(":");
                Integer targetEndTime1 = (Integer.parseInt(targetEndTimeForTime[0])*60)+Integer.parseInt(targetEndTimeForTime[1]);
                Integer timeBetwwenThem = targetEndTime1-targetStartTime1;
				if(timeBetwwenThem<15){
		            result.rejectValue("time", "time","Please,Enter at leat 15 Minutes");
				}
			}
		}
		if (result.hasErrors()) {
			model.addAttribute("appointmentForm", appointmentForm);
			model.addAttribute("id",request.getParameter("id"));
			model.addAttribute("parentId",request.getParameter("parentId"));
			model.addAttribute("parentType",request.getParameter("parentType"));
			model.addAttribute("salesMen", salesMen);
			model.addAttribute("contacts", contacts);
			model.addAttribute("appointmentForm", appointmentForm);
			//model.addAttribute("parentId", parentId);
			model.addAttribute("userDetails", userDetails);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "edit-appointment";
		}
		Appointment appointment = appointmentService.getAppointmentById(new Appointment(), Integer.parseInt(request.getParameter("id")));

		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		Date newDate;
		if(appointmentForm.getSalesMan() != null){
			appointment.setSalesMan(employeeService.getById(appointmentForm.getSalesMan()));
		}
		if(appointmentForm.getContactedPerson() != null){
			appointment.setContactedPerson(contactService.getContactById(appointmentForm.getContactedPerson()));
		}
		if(appointmentForm.getDay() != null){
			appointment.setDay(appointmentForm.getDay());
		}
		if(appointmentForm.getDate() != null){
			try {
				newDate = format.parse(appointmentForm.getDate());
				appointment.setDate(newDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(appointmentForm.getTime() != null){
			appointment.setTime(appointmentForm.getTime());
		}
		if( appointmentForm.getComment()!= null){
			appointment.setComment(appointmentForm.getComment());
		}
		if(appointmentForm.getNextFollowUp() != null){
			appointment.setNextFollowUp(Boolean.valueOf(appointmentForm.getNextFollowUp()));
		}
		if(appointmentForm.getNextFollowUpType() != null){
			appointment.setFollowUpType(appointmentForm.getNextFollowUpType());
		}
		if(appointmentForm.getNextFollowUpDate() != null&&!(appointmentForm.getNextFollowUpDate().equalsIgnoreCase(""))){
			try {
				newDate = format.parse(appointmentForm.getNextFollowUpDate());
				appointment.setFollowUpDate(newDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(appointmentForm.getNextFollowUpDetails() != null){
			appointment.setFollowUpDetails(appointmentForm.getNextFollowUpDetails());
		}
		if(appointmentForm.getDiscountDone() != null){
			appointment.setDiscountDone(appointmentForm.getDiscountDone());
		}
		if(appointmentForm.getCompetitorOffer() != null){
			appointment.setCompatitorOffer(appointmentForm.getCompetitorOffer());
		}
		if(appointmentForm.getDealClosingDone() != null){
			appointment.setDealClosingDone(appointmentForm.getDealClosingDone());
		}
		if(appointmentForm.getReplyPeriod() != null){
			appointment.setReplyPeriod(appointmentForm.getReplyPeriod());
		}
		if(appointmentForm.getDealClosingReason() != null){
			appointment.setDealClosingReason(appointmentForm.getDealClosingReason());
		}
		if(appointmentForm.getMeetingDone() != null){
			appointment.setMeetingDone(appointmentForm.getMeetingDone());
		}
		if(appointmentForm.getMeetingDuration() != null){
			appointment.setMeetingDuration(appointmentForm.getMeetingDuration());
		}
		if(appointmentForm.getCustomerNeed() != null){
			appointment.setCustomerNeed(appointmentForm.getCustomerNeed());
		}
		if(appointmentForm.getDemoDone() != null){
			appointment.setDemoDone(appointmentForm.getDemoDone());
		}
		if(appointmentForm.getOfferSubmitted() != null){
			appointment.setOfferSubmitted(appointmentForm.getOfferSubmitted());
		}
		if(appointmentForm.getConfirmationDateExpected() != null){
			appointment.setConfirmationDateExpected(appointmentForm.getConfirmationDateExpected());
		}
		if(appointmentForm.getDiscountDone() != null){
			appointment.setDiscountDone(appointmentForm.getDiscountDone());
		}
//		if(appointmentForm.getDealClosingDone() != null){
//			appointment.setDealClosingReason(dealClosingReason);
//		}
		if(appointmentForm.getParentId() != null){
			appointment.setParentId(appointmentForm.getParentId());
		}
		if(appointmentForm.getParentType() != null){
			appointment.setParentType("lead");
		}
		
//		appointment.setParentId(parentId);
//		appointment.setParentType(parentType);
//		appointment.setParentType("lead");
		appointment.setDateModified(new Date());
		appointmentService.saveOrUpdate(appointment);
		calendarService.appointmentEvent(appointment);
		model.addAttribute("userDetails", userDetails);
			//
			UpdateChatter updateChatter = new UpdateChatter();
			updateChatter.setAction_emp(userDetails);
			updateChatter.setDate(new Date());
			updateChatter.setFollowedId(appointment.getId());
			updateChatter.setFollowType(FOLLOWTYPE.Appointment.getType());
			updateChatter.setUpdateType(UPDATETYPE.Edited.getName());
			Post post = new Post();
			post.setPostDate(new Date());
			post.setPoster(userDetails);

			String postText = "<a href='/sales/s/view-appointment?id="+appointment.getId()+"'>i edited Appointment at Time:"+appointment.getTime()+ "And Date"+appointment.getTime()+ "And Date:"+appointmentForm.getDate()+"</a>";
			post.setPostText(postText);
			postService.addPost(post);	
			updateChatter.setPost(post);
			updateChatterService.add(updateChatter);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
		
		return "redirect:/s/viewLead.html?id="+appointment.getParentId();
	}

	@RequestMapping(value="/view-appointment",method=RequestMethod.GET)
	public String getViewAppointment(Model model,HttpSession session,HttpServletRequest request, ModelMap modelMap) {
		if (SecurityUtils.getSubject().hasRole("appointmentView")){
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Appointment appointment =appointmentService.getAppointmentById(new Appointment(),Integer.parseInt(request.getParameter("id"))); 
		
		
		model.addAttribute("id",request.getParameter("id"));
		model.addAttribute("parentId",request.getParameter("parentId"));
		model.addAttribute("parentType",request.getParameter("parentType"));
		model.addAttribute("appointment", appointment);
		//model.addAttribute("parentId", parentId);
		model.addAttribute("userDetails", userDetails);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "view-appointment";
		}
		else{
			return "accessDenied";
		}
	}
	
	
}
